

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>packnet_sfm.models.SemiSupModel &mdash; PackNet-SfM 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../configs/configs.html">Configs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../scripts/scripts.html">Scripts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trainers/trainers.html">Trainers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../datasets/datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../networks/networks.html">Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../losses/losses.html">Losses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../loggers/loggers.html">Loggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../geometry/geometry.html">Geometry</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils/utils.html">Utils</a></li>
</ul>
<p class="caption"><span class="caption-text">Contact</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://tri.global">Toyota Research Institute</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/packnet-sfm">PackNet-SfM GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/DDAD">DDAD GitHub</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">PackNet-SfM</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>packnet_sfm.models.SemiSupModel</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for packnet_sfm.models.SemiSupModel</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Toyota Research Institute.  All rights reserved.</span>

<span class="kn">import</span> <span class="nn">torch</span>

<span class="kn">from</span> <span class="nn">packnet_sfm.models.SelfSupModel</span> <span class="kn">import</span> <span class="n">SfmModel</span><span class="p">,</span> <span class="n">SelfSupModel</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.losses.supervised_loss</span> <span class="kn">import</span> <span class="n">SupervisedLoss</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.models.model_utils</span> <span class="kn">import</span> <span class="n">merge_outputs</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.depth</span> <span class="kn">import</span> <span class="n">depth2inv</span>


<div class="viewcode-block" id="SemiSupModel"><a class="viewcode-back" href="../../../models/models.SemiSupModel.html#packnet_sfm.models.SemiSupModel.SemiSupModel">[docs]</a><span class="k">class</span> <span class="nc">SemiSupModel</span><span class="p">(</span><span class="n">SelfSupModel</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Model that inherits a depth and pose networks, plus the self-supervised loss from</span>
<span class="sd">    SelfSupModel and includes a supervised loss for semi-supervision.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    supervised_loss_weight : float</span>
<span class="sd">        Weight for the supervised loss</span>
<span class="sd">    kwargs : dict</span>
<span class="sd">        Extra parameters</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">supervised_loss_weight</span><span class="o">=</span><span class="mf">0.9</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># Initializes SelfSupModel</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="c1"># If supervision weight is 0.0, use SelfSupModel directly</span>
        <span class="k">assert</span> <span class="mf">0.</span> <span class="o">&lt;</span> <span class="n">supervised_loss_weight</span> <span class="o">&lt;=</span> <span class="mf">1.</span><span class="p">,</span> <span class="s2">&quot;Model requires (0, 1] supervision&quot;</span>
        <span class="c1"># Store weight and initializes supervised loss</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">supervised_loss_weight</span> <span class="o">=</span> <span class="n">supervised_loss_weight</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_supervised_loss</span> <span class="o">=</span> <span class="n">SupervisedLoss</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># Pose network is only required if there is self-supervision</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_network_requirements</span><span class="p">[</span><span class="s1">&#39;pose_net&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">supervised_loss_weight</span> <span class="o">&lt;</span> <span class="mi">1</span>
        <span class="c1"># GT depth is only required if there is supervision</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_train_requirements</span><span class="p">[</span><span class="s1">&#39;gt_depth&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">supervised_loss_weight</span> <span class="o">&gt;</span> <span class="mi">0</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">logs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return logs.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="o">**</span><span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">logs</span><span class="p">,</span>
            <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">_supervised_loss</span><span class="o">.</span><span class="n">logs</span>
        <span class="p">}</span>

<div class="viewcode-block" id="SemiSupModel.supervised_loss"><a class="viewcode-back" href="../../../models/models.SemiSupModel.html#packnet_sfm.models.SemiSupModel.SemiSupModel.supervised_loss">[docs]</a>    <span class="k">def</span> <span class="nf">supervised_loss</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inv_depths</span><span class="p">,</span> <span class="n">gt_inv_depths</span><span class="p">,</span>
                        <span class="n">return_logs</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">progress</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the supervised loss.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        inv_depths : torch.Tensor [B,1,H,W]</span>
<span class="sd">            Predicted inverse depth maps from the original image</span>
<span class="sd">        gt_inv_depths : torch.Tensor [B,1,H,W]</span>
<span class="sd">            Ground-truth inverse depth maps from the original image</span>
<span class="sd">        return_logs : bool</span>
<span class="sd">            True if logs are stored</span>
<span class="sd">        progress :</span>
<span class="sd">            Training progress percentage</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        output : dict</span>
<span class="sd">            Dictionary containing a &quot;loss&quot; scalar a &quot;metrics&quot; dictionary</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_supervised_loss</span><span class="p">(</span>
            <span class="n">inv_depths</span><span class="p">,</span> <span class="n">gt_inv_depths</span><span class="p">,</span>
            <span class="n">return_logs</span><span class="o">=</span><span class="n">return_logs</span><span class="p">,</span> <span class="n">progress</span><span class="o">=</span><span class="n">progress</span><span class="p">)</span></div>

<div class="viewcode-block" id="SemiSupModel.forward"><a class="viewcode-back" href="../../../models/models.SemiSupModel.html#packnet_sfm.models.SemiSupModel.SemiSupModel.forward">[docs]</a>    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">return_logs</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">progress</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Processes a batch.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        batch : dict</span>
<span class="sd">            Input batch</span>
<span class="sd">        return_logs : bool</span>
<span class="sd">            True if logs are stored</span>
<span class="sd">        progress :</span>
<span class="sd">            Training progress percentage</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        output : dict</span>
<span class="sd">            Dictionary containing a &quot;loss&quot; scalar and different metrics and predictions</span>
<span class="sd">            for logging and downstream usage.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">training</span><span class="p">:</span>
            <span class="c1"># If not training, no need for self-supervised loss</span>
            <span class="k">return</span> <span class="n">SfmModel</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">supervised_loss_weight</span> <span class="o">==</span> <span class="mf">1.</span><span class="p">:</span>
                <span class="c1"># If no self-supervision, no need to calculate loss</span>
                <span class="n">self_sup_output</span> <span class="o">=</span> <span class="n">SfmModel</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">)</span>
                <span class="n">loss</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mf">0.</span><span class="p">])</span><span class="o">.</span><span class="n">type_as</span><span class="p">(</span><span class="n">batch</span><span class="p">[</span><span class="s1">&#39;rgb&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Otherwise, calculate and weight self-supervised loss</span>
                <span class="n">self_sup_output</span> <span class="o">=</span> <span class="n">SelfSupModel</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">)</span>
                <span class="n">loss</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">supervised_loss_weight</span><span class="p">)</span> <span class="o">*</span> <span class="n">self_sup_output</span><span class="p">[</span><span class="s1">&#39;loss&#39;</span><span class="p">]</span>
            <span class="c1"># Calculate and weight supervised loss</span>
            <span class="n">sup_output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">supervised_loss</span><span class="p">(</span>
                <span class="n">self_sup_output</span><span class="p">[</span><span class="s1">&#39;inv_depths&#39;</span><span class="p">],</span> <span class="n">depth2inv</span><span class="p">(</span><span class="n">batch</span><span class="p">[</span><span class="s1">&#39;depth&#39;</span><span class="p">]),</span>
                <span class="n">return_logs</span><span class="o">=</span><span class="n">return_logs</span><span class="p">,</span> <span class="n">progress</span><span class="o">=</span><span class="n">progress</span><span class="p">)</span>
            <span class="n">loss</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">supervised_loss_weight</span> <span class="o">*</span> <span class="n">sup_output</span><span class="p">[</span><span class="s1">&#39;loss&#39;</span><span class="p">]</span>
            <span class="c1"># Merge and return outputs</span>
            <span class="k">return</span> <span class="p">{</span>
                <span class="s1">&#39;loss&#39;</span><span class="p">:</span> <span class="n">loss</span><span class="p">,</span>
                <span class="o">**</span><span class="n">merge_outputs</span><span class="p">(</span><span class="n">self_sup_output</span><span class="p">,</span> <span class="n">sup_output</span><span class="p">),</span>
            <span class="p">}</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Toyota Research Institute (TRI)

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>